package com.gmss.crawler.utils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 正则工具类
 */
public class RegexUtils {

    // 验证IP是否正确
    private static final String IP_REGEX = "\\d{1,3}[0-255]\\.\\d{1,3}[0-255]\\.\\d{1,3}[0-255]\\.\\d{1,3}[0-255]";
    // 检查字符串中是否包含ip
//    private static final String CHECK_IP_REGEX = "\\d{1,3}[0-255]+\\.\\d{1,3}[0-255]+\\.\\d{1,3}[0-255]+\\.\\d{1,3}[0-255]+";
    private static final String CHECK_IP_REGEX = "\\d+\\.\\d+\\.\\d+\\.\\d+";
    // 获取script标签里的内容
    private static final String SCRIPT_REGEX_JS = "(<script(.*?)>)(.|\n)*?(</script>)";
    // 获取style标签里的内容
    private static final String STYLE_REGEX_JS = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
    // 里所有[]内的内容（包含[]）正则
    private static final String STYLE_REGEX_ZHONG = "\"\\[.*?\\]\"";
    // 匹配http:/https://URL
    private static final String HTTP_REGEX = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*";
    // 查询文本中所有包含$${}的内容
    public static final String ALL_REGEX = "\\$\\$\\{[^\\{\\}]{1,}\\}";
    // 查询文本中所有包含$${}的内容
    public static final String ALL_REGEX_TWO = "\\$\\$\\{(.*?)}";
    // 查询文本中所有包含[]的内容
    public static final String ALL_ZHONG_REGEX = "\\[.*?\\]";
    // // 查询文本中所有不包含[]的内容,如：A[BC]D,只取BC
    public static final String ALL_ZHONG_REGEX_TWO = "(?<=\\[)(.*?)(?=\\])";
    // 查询文本中所有包含()的内容,如：A(BC)D,只取(BC)
    public static final String ALL_KUA_REGEX = "\\(.*?\\)";
    // 查询文本中所有不包含()的内容,如：A(BC)D,只取BC
    public static final String ALL_KUA_REGEX_TWO = "(?<=\\()(.+?)(?=\\))";
    // 邮箱正则
    public static final String MAIL_REGEX = "^[a-z0-9A-Z]+[- | a-z0-9A-Z \\. _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    // 查询字符串中的英文和“.”“-”
    public static final String ENGLISH = "[a-zA-z.-]";
    // 只保留中文和“·”
    public static final String CHINA = "[^\u4E00-\u9FA5·]";
    // 数字点，1.，2.等
    public static final String NUMBER_DIAN = "\\d+\\.";
    public static final String DEGREE_REGEX = "(?<=获)(.*?)(?=学位)";
    public static final String DEGREE_REGEX_TWO = "(?<=获)(.*?)(?=士)";
    // 获得纽约大学计算机博士学位后,获得哥伦比亚大学数学博士学位。只取第二次出现并匹配的数据
//    public static final String DEGREE_REGEX = "(?!.获\\1)(.+?)(?=学位)";
    // 获取在*工作之间的内容，但是不包含“在”,“工作”
    //    public static final String WORK_REGEX = "在.*工作";
    public static final String WORK_REGEX = "(?<=在)(.+?)(?=工作)";
    //获取在*工作之间的内容，但是不包含“在”,“工作”，“进行[研究、科研]等工作”
    public static final String WORK_REGEX2 = "(?<=到)(.+?)(?=进行(.*?)工作)";
    public static final String WORK_REGEX3 = "(?<=往)(.+?)(?=进行(.*?)工作)";
    public static final String WORK_REGEX4 = "(?<=到)(.+?)(?=工作)";
    public static final String SCHOOL_REGEX = "(?<=毕业于)(.+?)(?=大学)";
    public static final String SCHOOL_REGEX1 = "(?<=毕业)(.+?)(?=大学)";
    public static final String SCHOOL_REGEX2 = "(?<=毕业于)(.+?)(?=学校)";
    public static final String SCHOOL_REGEX3 = "(?<=毕业于)(.+?)(?=学院)";
    // 从文本中获取“, 至 出生”之间的日期
    public static final String BIRTHDAY = "(?<=,)(.+?)(?=出生)";
    // 从文本中获取“出生”之前的字符串
    public static final String BIRTHDAY_TWO = "(?<=)(.+?)(?=出生)";
    // 从文本中获取“, 至 年生”之前的字符串
    public static final String BIRTHDAY_THREE = "(?<=,)(.+?)(?=年生)";
    // 从文本中获取“年生”之前的字符串
    public static final String BIRTHDAY_FOUR = "(?<=)(.+?)(?=年生)";
    // 从文本中获取“年月”之前的字符串
    public static final String BIRTHDAY_MONTH = "(?<=年)(.+?)(?=月)";
    // 从文本中获取“月日”之前的字符串
    public static final String BIRTHDAY_DAY = "(?<=月)(.+?)(?=日)";
    // 从文本中获取“生于 至 ,”之前的字符串
    public static final String BIRTHPLACE = "(?<=出生于)(.+?)(?=,)";
    public static final String BIRTHPLACE_TWO = "(?<=生于)(.+?)(?=,)";
    // 从文本中获取“第一个, 至 籍华人”之前的字符串
    public static final String NATIONALITY = "(?<=,)(.+?)(?=籍华人)";
//    public static final String WORK_REGEX5 = "(?<=任)(.+?)(?=教授)";
//    public static final String WORK_REGEX6 = "(?<=进入)(.+?)(?=工作)";
//    public static final String WORK_REGEX7 = "(?<=进入)(.+?)(?=进行(.*?)工作)";
//    public static final String WORK_REGEX8 = "(?<=入)(.+?)(?=工作)";


    // 从文本中获取“省”之前的字符串
    public static final String PROVINCE_REGEX = "(?<=)(.+?)(?=省)";
    public static final String CITY_REGEX = "(?<=)(.+?)(?=市)";
    public static final String REGION_REGEX = "(?<=)(.+?)(?=区)";
    public static final String COUNTY_REGEX = "(?<=)(.+?)(?=县)";
    // 从文本中获取“省市”之前的字符串
    public static final String PROVINCE_CITY_REGEX = "(?<=省)(.+?)(?=市)";
    // 从文本中获取“市区”之前的字符串
    public static final String CITY_REGION_REGEX = "(?<=市)(.+?)(?=区)";
    // 从文本中获取“市县”之前的字符串
    public static final String CITY_COUNTY_REGEX = "(?<=市)(.+?)(?=县)";
    // 科研诚信案件-第一作者
    public static final String FIRST_AUTHOR_REGEX = "(?<=对第一作者)(.+?)(?=：)";
    public static final String FIRST_AUTHOR_REGEX_TWO = "(?<=第一作者)(.+?)(?=、)";
    public static final String FIRST_AUTHOR_REGEX_THREE = "(?<=医院)(.+?)(?=为第一作者)";
    public static final String FIRST_AUTHOR_REGEX_FOUR = "(?<=)(.+?)(?=为第一作者)";
    public static final String FIRST_AUTHOR_REGEX_SIX = "(?<=第一作者)(.+?)(?=作出如下处理：)";
    public static final String FIRST_AUTHOR_REGEX_SIX_TWO = "(?<=对第一作者)(.+?)(?=作出处理如下：)";
    public static final String FIRST_AUTHOR_REGEX_SEVEN = "(?<=医院)(.+?)(?=论文)";
    public static final String FIRST_AUTHOR_REGEX_EIGHT = "(?<=，)(.+?)(?=为第一作者)";
    public static final String FIRST_AUTHOR_REGEX_NINE = "(?<=科)(.+?)(?=为第一作者)";
    public static final String FIRST_AUTHOR_REGEX_TEN = "(?<=、)(.+?)(?=为第一作者)";
    public static final String FIRST_AUTHOR_REGEX_TEN_TWO = "(?<=集团)(.+?)(?=为第一作者)";
    // 科研诚信案件-并列第一作者
    public static final String AND_FIRST_AUTHOR_REGEX = "(?<=对并列第一作者)(.+?)(?=：)";
    public static final String AND_FIRST_AUTHOR_REGEX_TWO = "(?<=并列第一作者)(.+?)(?=、)";
    public static final String AND_FIRST_AUTHOR_REGEX_THREE = "(?<=医院)(.+?)(?=为共同第一作者)";
//    public static final String AND_FIRST_AUTHOR_REGEX_FOUR = "(?<=)(.+?)(?=并列第一作者)";
    public static final String AND_FIRST_AUTHOR_REGEX_FIVE = "(?<=并列第一作者)(.+?)(?=作出如下处理：)";
    public static final String AND_FIRST_AUTHOR_REGEX_FIVE_TWO = "(?<=共同第一作者)(.+?)(?=作出如下处理：)";
    public static final String AND_FIRST_AUTHOR_REGEX_SIX = "(?<=共同第一作者)(.+?)(?=：)";
    public static final String AND_FIRST_AUTHOR_REGEX_SEVEN = "(?<=、)(.+?)(?=为并列第一作者)";
    public static final String AND_FIRST_AUTHOR_REGEX_EIGHT = "(?<=，)(.+?)(?=为并列第一作者)";
    public static final String AND_FIRST_AUTHOR_REGEX_TEN = "(?<=医院)(.+?)(?=共同第一作者)";
    public static final String AND_FIRST_AUTHOR_REGEX_NINE = "(?<=，)(.+?)(?=共同第一作者)";
    public static final String AND_FIRST_AUTHOR_REGEX_ELEVEN = "(?<=对并列第一作者)(.+?)(?=通报批评)";


    // 科研诚信案件-第二作者
    public static final String SECOND_AUTHOR_REGEX = "(?<=对第二作者)(.+?)(?=：)";
    public static final String SECOND_AUTHOR_REGEX_TWO = "(?<=第二作者)(.+?)(?=作出如下处理：)";
    public static final String SECOND_AUTHOR_REGEX_THREE = "(?<=第二作者)(.+?)(?=、)";
    // 科研诚信案件-第三作者
    public static final String THIRD_AUTHOR_REGEX = "(?<=对第三作者：)(.+?)(?=：)";
    public static final String THIRD_AUTHOR_REGEX_TWO = "(?<=第三作者)(.+?)(?=：)";
    public static final String THIRD_AUTHOR_REGEX_THREE = "(?<=第三作者)(.+?)(?=、)";
    public static final String THIRD_AUTHOR_REGEX_FOUR = "(?<=第三作者)(.+?)(?=作出处理如下：)";
    // 科研诚信案件-第四作者
    public static final String FOUR_AUTHOR_REGEX = "(?<=对第四作者：)(.+?)(?=：)";
    public static final String FOUR_AUTHOR_REGEX_TWO = "(?<=第四作者)(.+?)(?=：)";
    public static final String FOUR_AUTHOR_REGEX_THREE = "(?<=第四作者)(.+?)(?=、)";
    // 科研诚信案件-第五作者
    public static final String FIVE_AUTHOR_REGEX = "(?<=对第五作者：)(.+?)(?=：)";
    public static final String FIVE_AUTHOR_REGEX_TWO = "(?<=第五作者)(.+?)(?=：)";
    public static final String FIVE_AUTHOR_REGEX_THREE = "(?<=第五作者)(.+?)(?=、)";
    // 科研诚信案件-第六作者
    public static final String SIX_AUTHOR_REGEX = "(?<=对第六作者：)(.+?)(?=：)";
    public static final String SIX_AUTHOR_REGEX_TWO = "(?<=第六作者)(.+?)(?=：)";
    public static final String SIX_AUTHOR_REGEX_THREE = "(?<=第六作者)(.+?)(?=、)";
    // 科研诚信案件-第一通讯作者
    public static final String FIRST_COMMUNICATION_AUTHOR_REGEX = "(?<=对通讯作者)(.+?)(?=：)";
    public static final String COMMUNICATION_AUTHOR_REGEX = "(?<=通讯作者)(.+?)(?=、)";
    public static final String COMMUNICATION_AUTHOR_REGEX_ONE = "(?<=通讯作者)(.+?)(?=作出如下处理：)";
    public static final String COMMUNICATION_AUTHOR_REGEX_ONE_TWO = "(?<=对通讯作者)(.+?)(?=作出如下处理：)";
    public static final String COMMUNICATION_AUTHOR_REGEX_ONE_OTHER = "(?<=对通讯作者)(.+?)(?=作出处理如下：)";
    //    public static final String COMMUNICATION_AUTHOR_REGEX_ONE_LAST = "(?!.通讯作者\\1)(.+?)(?=作出如下处理：)";
    public static final String COMMUNICATION_AUTHOR_REGEX_ONE_LAST = "(?<=通讯作者)(.+?)(?= )";
    public static final String COMMUNICATION_AUTHOR_REGEX_TWO = "(?<=医院)(.+?)(?=为通讯作者)";
    public static final String COMMUNICATION_AUTHOR_REGEX_THREE = "(?<=)(.+?)(?=为通讯作者)";
    public static final String COMMUNICATION_AUTHOR_REGEX_FOUR = "(?<=医院)(.+?)(?=为第一通讯作者)";
    public static final String COMMUNICATION_AUTHOR_REGEX_FIVE = "(?<=)(.+?)(?=作出如下处理：)";
    public static final String COMMUNICATION_AUTHOR_REGEX_SIX = "(?<=)(.+?)(?=作出如下处理)";
    public static final String COMMUNICATION_AUTHOR_REGEX_SEVEN = "(?<=医院)(.+?)(?=为共同通讯作者)";


    // 科研诚信案件-其他作者
    public static final String OTHER_AUTHOR_REGEX = "(?<=对其他作者)(.+?)(?=：)";
    public static final String OTHER_AUTHOR_REGEX_ONE = "(?<=其他作者)(.+?)(?=作出如下处理：)";
    public static final String OTHER_AUTHOR_REGEX_TWO = "(?<=参与作者)(.+?)(?=：)";
    public static final String OTHER_AUTHOR_REGEX_THREE = "(?<=参与作者)(.+?)(?=作出如下处理：)";
    // 取“对其他作者焦慧、刘颖颖：”数据,要求取：焦慧、刘颖颖，但只能取到焦慧。
//    public static final String OTHER_AUTHOR_REGEX_THREE = "(?<=对其他作者)(.*?)(?=、(.*?)：)";

    // 科研诚信案件-第一兼通讯作者
    public static final String FIRST_AND_REPORT_AUTHOR_REGEX = "(?<=第一兼通讯作者)(.+?)(?=：)";
    public static final String FIRST_AND_REPORT_AUTHOR_REGEX_TWO = "(?<=第一兼通讯作者)(.+?)(?=作出如下处理：)";
    public static final String FIRST_AND_PEPORT_AUTHOR_REGEX_THREE = "(?<=医院)(.+?)(?=为第一兼通讯作者)";

    // 科研诚信案件-单位
    public static final String COMPANY_REGEX = "(?<=)(.+?)(?=论文“)";
    public static final String COMPANY_REGEX_TWO = "(?<=)(.+?)(?=医院)";
    public static final String COMPANY_REGEX_THREE = "(?<=)(.+?)(?=保健院)";

    public static final String PAPER_REGEX = "(?<=论文“)(.+?)(?=”)";
    public static final String NOTIFICATION_LINK_REGEX = "(?<=（通报链接：)(.+?)(?=）)";
    public static final String HANDLE_RESULT_REGEX = "(?<=经查，)(.+?)(?=（通报链接)";

    public static void main(String[] args) {


        String h = "https://www.cae.cn/cae/html/main/col329/column_329_1.html#anchor_point";
        System.out.println(isHttp(h));

//        String A = "陈建二，广州大学计算机科学与网络工程学院教授、博士生导师，中华人民共和国教育部第二批“长江学者”特聘教授。曾主持多个重点学术科研项目、担任《中国科学：信息科学》、《计算机学报》等多个期刊杂志编委、发表数百篇论文、获得多个奖项。1982年毕业于中南矿冶学院计算机专业，后获得纽约大学计算机博士学位、哥伦比亚大学数学博士学位。曾任美国Texas A&M大学计算机系教授。";
        String A = "陈建二，后获得纽约大学计算机博士学位、哥伦比亚大学数学博士学位。曾任美国Texas A&M大学计算机系教授。";
        String S = RegexUtils.getTextContent(A, RegexUtils.DEGREE_REGEX);
        System.out.println(S);
        String SG = RegexUtils.getTextContent(S, RegexUtils.DEGREE_REGEX_TWO);
        System.out.println(SG);

//        replaceIp();

//        String b = "钱冬,男,";
//        String b = "[\"1962年出生于蒲江县寿安镇七星村,1970年进入七星村小学学习,1975年至1979年在金家祠中学接受初中和高中教育,自幼学习成绩优异\",\"1983年获南京航空航天大学学士学位,\",\"1986年获华南理工大学硕士学位,\",\"1995年获澳大利亚悉尼大学博士学位,\",\"1996年和1997年在英国帝国理工学院和丹麦阿尔堡大学做博士后研究,\",\"1998年起在北京大学力学与工程科学系工作\"]";
//        String birthday = RegexUtils.getTextContent(b, RegexUtils.BIRTHDAY);
//        if (StringUtils.isBlank(birthday))
//            birthday = RegexUtils.getTextContent(b, RegexUtils.BIRTHDAY_TWO);
//        String str = CommonUtils.getBirthdayByString(birthday);
//        System.out.println(str);
//
//        String birthplace = RegexUtils.getTextContent(b, RegexUtils.BIRTHPLACE);
//        System.out.println(birthplace);

//        String firstCell = "1992年8月到紫金山天文台进行dsfdsdsfs工作";
//        firstCell = "分配到郑州的邮电部设计院(现为中讯邮电咨询设计院)工作";
//        PersonInfo personInfo = new PersonInfo();
//        String aa = getTextContent(firstCell, ALL_KUA_REGEX_TWO);
//        System.out.println(aa);
//        aa = RegexUtils.replaceData(firstCell, RegexUtils.ALL_KUA_REGEX, "");
//        System.out.println(aa);

//        replaceIp();


//        String firstCellHzDec =  firstCell.replaceAll("[^\u4E00-\u9FA5·]", "");
//        System.out.println(firstCellHzDec);
//
////        String text = "常进，男，1966年7月生，[1]籍贯江苏泰兴，天文学家[2]，中国科学院院士[3]，研究员，博士，" +
////                "博士生导师，中国科学院紫金山天文[5-4]台台长，中国科学院国家天文台台长，中国科学院暗物质与空间天文重点实验室主任，";
////               ;
////        System.out.println(replaceData(text, "\\[.*?\\]", ""));
////        System.out.println();
//        String name = CommonUtils.replaceBlank("菲立普 · 希阿雷 Philippe G. Ciarlet");
//        System.out.println(replaceData(name,ENGLISH,""));
//
//
//        getTextContent(ALL_REGEX);
//        replaceIp();
//
        String crawlerPath2 = "https://baike.baidu.com/search?word=$${1}&pn=$${2}&rn=$${3}&enc=utf8";
        System.out.println(replaceData(crawlerPath2, "\\$\\$\\{1}", "1"));
        System.out.println(replaceData(crawlerPath2, "\\$\\$\\{2}", "2"));
        System.out.println(replaceData(crawlerPath2, "\\$\\$\\{3}", "3"));
        System.out.println(getTextContent222());
//        String namePath = RegexUtils.replaceData(crawlerPath2, RegexUtils.ALL_REGEX, "常进");
//         namePath = RegexUtils.replaceData(namePath, RegexUtils.ALL_REGEX_TWO, "2222");
//         namePath = RegexUtils.replaceData(namePath, RegexUtils.ALL_REGEX_THREE, "3333");
//        System.out.println(namePath);

//
//        System.out.println(getUrl("dsafdsafdsfasdfa dsfdsafhttps://pictestll.oss-cn-beijing.aliyuncs.com/test001/003/85e1a9b0ddc740419d7b75db9269cd20.png111111111"));
//
//
//        System.out.println(validateIp("192.168.220.197"));
//
//
//        System.out.println(getDomainName("http://localhost:8080/test/session"));//http://localhost:8080/
//        System.out.println(getDomainName("http://localhost:8080/test/session/", 3)); //http://localhost:8080/test/session/
//        String regex = "\\://";
//        String text = "http://127.0.0.1:8080/test/session/";
////        System.out.println(IsMatch(text,regex));
//        System.out.println(getUrl(text));

//        try{
//            String test = "http://127.0.0.1:8080/test/session/";
//            System.out.println(test.matches("^(http|https|ftp)://"));
//        } finally{
//
//        }
//        String completePerson = "sdskfls(123),dkslfisod(5)";
//        getTextContent(completePerson,ALL_KUA_REGEX);
//
//        completePerson = completePerson.replaceAll("\\(","").replaceAll("\\)","");
//        System.out.println(completePerson);


    }

    /**
     * 验证是否包含http/https
     *
     * @param str
     * @return true：包含，false：不包含
     */
    public static boolean isHttp(String str) {
        Pattern p = Pattern.compile(HTTP_REGEX);
        Matcher m = p.matcher(str);
        String data = "";
        while (m.find()) {
            return true;
        }
        return false;
    }

    public static String getTextContent(String str, String regex) {
        // ALL_REGEX 获取{{}}的内容
//        String str = "今天天{(3)}气不错，阳光挺{(10)}好的····',如何能把'{(3)},{(10)}........$FD........{12},,,,$${select,btnnav,打开内容类型,false,10,~label:外链；value:btnnavurl~#~label:内容；value:btnnavcontent~#~label:js；value:btnnavjs~})";
//        String ll = "select,btnnav,打开内容类型,false,10,~label:外链；value:btnnavurl~~#~label:内容；value:btnnavcontent~~#~label:js；value:btnnavjs~~";
//        regex = ALL_KUA_REGEX;
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        String data = "";
        while (m.find()) {
            data = m.group(0);
//            System.out.println(m.group(0));
        }
        return data;
    }

    public static String getTextContent222() {
        // ALL_REGEX 获取{{}}的内容
        String str = "今天天{(3)}气不错，阳光挺{(10)}好的····',如何能把'{(3)},{(10)}........$FD........{12},,,,$${select,btnnav,打开内容类型,false,10,~label:外链；value:btnnavurl~#~label:内容；value:btnnavcontent~#~label:js；value:btnnavjs~})";
        String ll = "select,btnnav,打开内容类型,false,10,~label:外链；value:btnnavurl~~#~label:内容；value:btnnavcontent~~#~label:js；value:btnnavjs~~";
        String regex = ALL_KUA_REGEX;
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        String data = "";
        while (m.find()) {
            String patternStr = m.group();
//            System.out.println(patternStr);
        }
        return data;
    }

    /**
     * 根据正则去除字符串中的字符
     *
     * @param text  文本数据
     * @param regex 匹配文本数据的正则
     * @param data  匹配到后，把data值替换到文本里
     * @return
     */
    public static String replaceData(String text, String regex, String data) {
//        String crawlerPath2 = "https://baike.baidu.com/search?word=$${1}&pn=0&rn=0&enc=utf8";
//        String namePath = RegexUtils.replaceData(crawlerPath2, RegexUtils.ALL_REGEX, "常进");
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            return text.replaceAll("(?>" + regex + ")", data);
        }
        return text;
    }


    public static String getUrl(String text) {
        //noinspection RegExpRedundantEscape
//        Pattern p = Pattern.compile("((http|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?", Pattern.CASE_INSENSITIVE);
        Pattern p = Pattern.compile("((http[s]{0,1})://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?", Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(text);
        boolean isMatcher = matcher.find();
        if (isMatcher) {
            return matcher.group();
        }
        return "";
    }

    private static boolean IsMatch(String s, String pattern) {
        try {
            Pattern patt = Pattern.compile(pattern);
            Matcher matcher = patt.matcher(s);
            while (matcher.find()) {
//                System.out.println(s.substring(s.indexOf(matcher.group()), s.indexOf("\\.")));
//                System.out.println(s.substring(s.indexOf(matcher.group()), s.indexOf("\\.")));
                System.out.println(s.indexOf(matcher.group()) + "----" + s.indexOf("\\."));
                System.out.println(matcher.group());

            }
            return matcher.matches();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取网络的根域名
     *
     * @param url
     * @return
     */
    public static String getDomainName(String url) {
        String pattern = "/(?!/)";
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(url);
        ArrayList<Integer> list = new ArrayList<>();
        while (matcher.find()) {
            list.add(matcher.start());
        }
        return url.substring(0, list.get(1) + 1);
    }

    /**
     * 根据 index 来返回几级域名<br/>
     * 如果 index为0，则返回顶级域名，如http://localhost:8080/。<br/>
     * 如果index大于url所包含的最大域名级数就返回url<br/>
     *
     * @param url
     * @param index 域名的级别，0为顶级域名，以此类推。
     * @return
     */
    public static String getDomainName(String url, int index) {
        String pattern = "/(?!/)";
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(url);
        ArrayList<Integer> list = new ArrayList<>();
        while (matcher.find()) {
            list.add(matcher.start());
        }

        try {
            //如果index大于url所包含的最大域名级数就返回url
            //这里面list的size减一就是这个url的最大级数，因为list包含一个/是url中http://的/。所以不应该算数
            if (index >= list.size() - 1) {
                return url;
            } else if (index < 0) {
                return null;
            } else {
                return url.substring(0, list.get(index + 1) + 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 验证IP是否正确
     *
     * @param ip
     * @return true：正确，false：错误
     */
    private static boolean validateIp(String ip) {

        String regex = "\\d{1,3}[0-255]\\.\\d{1,3}[0-255]\\.\\d{1,3}[0-255]\\.\\d{1,3}[0-255]";
        Pattern pattern = Pattern.compile(regex);    // 编译正则表达式
        Matcher matcher = pattern.matcher(ip);    // 创建给定输入模式的匹配器
        return matcher.matches();

//        Pattern pattern = Pattern.compile(IP_REGEX);
//        Matcher matcher = pattern.matcher(ip);
//        if (matcher.find())
//            return true;
//        else
//            return false;
    }

    /**
     * 正则检查字符串中是否包含IP，如果包含进行替换
     */
    public static void replaceIp() {
        String regex = "\\d+\\.\\d+\\.\\d+\\.\\d+";
        Pattern pattern = Pattern.compile(regex);
//		Matcher matcher = pattern.matcher("MessageActionQueue_192.168.10.1");
        String ipStr = "|fl=pw|mo=manual|ia=192.168.220.197|ma=00:07:3E:91:48:D6|ca=|cs=|cn=ceshi1|pw=1|sec=|";
        Matcher matcher = pattern.matcher(ipStr);

        if (matcher.find()) {
            String ip = matcher.group(0);
//            System.out.println("ip: " + ip);
            // 匹配到的字符串后添加
            System.out.println(ipStr.replaceAll("(?<=" + ip + ")", " 1.1.1.1"));
            // 匹配到的字符串前添加
            System.out.println(ipStr.replaceAll("(?=" + ip + ")", " 1.1.1.1"));
            // 匹配到的字符串进行替换，包含＝
            System.out.println(ipStr.replaceAll("(?>=" + ip + ")", " 1.1.1.1"));
            // 匹配到的字符串进行替换，不包含＝
            System.out.println(ipStr.replaceAll("(?>" + ip + ")", " 1.1.1.1"));
//			System.out.println(ipStr.replaceAll("ia=(?"+ip+")", "ia="+"1.1.1.1"));
        } else {
            System.out.println("no match");
        }
    }

    /**
     * 统计字符，在字符串中出现的次数
     */
    public static int countNumberByString(String text, String regex) {
        Matcher matcher = Pattern.compile(regex).matcher(text);
        int count = 0;
        while (matcher.find()) {
            count++;
        }
        return count;
    }


}
